/*
 * Copyright (C) 2016 venshine.cn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.wx.wheelview.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.vjwealth.android.nos.R;
import com.vjwealth.android.nos.entity.MapData;
import com.wx.wheelview.adapter.ArrayWheelAdapter;

import java.util.Arrays;
import java.util.List;

/**
 * 滚轮对话框
 *
 * @author venshine
 */
public class WheelViewDialog<T> implements View.OnClickListener {

    private TextView mTitle;

    private TextView mCancelBtn;
    private TextView mOKBtn;

    private WheelView<MapData> mWheelView;

    private WheelView.WheelViewStyle mStyle;

    private Dialog mDialog;

    private Context mContext;

    private OnDialogItemClickListener mOnDialogItemClickListener;

    private OnDialogBtnClickListener mOnDialogBtnClickListener;

    private int mSelectedPos;

    private T mSelectedText;

    private MapData mDataBean;

    public WheelViewDialog(Context context) {
        mContext = context;
        init();
    }

    private void init() {
        mDialog = new Dialog(mContext, R.style.time_dialog);
        mDialog.setCancelable(false);
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.setContentView(R.layout.wheelview_dialog);
        mCancelBtn = (TextView) mDialog.findViewById(R.id.dialogTitleCancelBtn);
        mOKBtn = (TextView) mDialog.findViewById(R.id.dialogTitleOKBtn);
        mTitle = (TextView) mDialog.findViewById(R.id.dialogTitleNameTV);
        mWheelView = (WheelView<MapData>) mDialog.findViewById(R.id.wheelView);
        mWheelView.setSkin(WheelView.Skin.Common);
        mWheelView.setWheelAdapter(new ArrayWheelAdapter<MapData>(mContext));
        mStyle = new WheelView.WheelViewStyle();
        mStyle.textColor = Color.parseColor("#E6D19C");
        mStyle.textSize = 14;
        mStyle.backgroundColor = Color.parseColor("#0D0D0D");
        mStyle.selectedTextZoom = 1.2f;
        mWheelView.setStyle(mStyle);

        mWheelView.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener<MapData>() {
            @Override
            public void onItemSelected(int position, MapData text) {
                mSelectedPos = position;
                mDataBean = text;
            }
        });
        mCancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnDialogBtnClickListener.onCancelBtnClick();
            }
        });
        mOKBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnDialogBtnClickListener.onComfirmBtnClick(mDataBean);
            }
        });

        Window window = mDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = dm.widthPixels;
        window.setAttributes(lp);
        mDialog.show();
    }

    /**
     * 点击事件
     *
     * @param onDialogItemClickListener
     * @return
     */
    public WheelViewDialog setOnDialogItemClickListener(OnDialogItemClickListener onDialogItemClickListener) {
        mOnDialogItemClickListener = onDialogItemClickListener;
        return this;
    }

    /**
     * 设置标题
     *
     * @param title
     * @return
     */
    public WheelViewDialog setTitle(String title) {
        mTitle.setText(title);
        return this;
    }

    /**
     * 设置标题颜色
     *
     * @param color
     * @return
     */
    public WheelViewDialog setTextColor(int color) {
        mTitle.setTextColor(color);
        return this;
    }

    /**
     * 设置标题大小
     *
     * @param size
     * @return
     */
    public WheelViewDialog setTextSize(int size) {
        mTitle.setTextSize(size);
        return this;
    }

    /**
     * 设置按钮文本
     *
     * @param text
     * @return
     */
    public WheelViewDialog setCancelBtnText(String text) {
        mCancelBtn.setText(text);
        return this;
    }


    /**
     * 设置数据项显示个数
     *
     * @param count
     */
    public WheelViewDialog setCount(int count) {
        mWheelView.setWheelSize(count);
        return this;
    }

    /**
     * 数据项是否循环显示
     *
     * @param loop
     */
    public WheelViewDialog setLoop(boolean loop) {
        mWheelView.setLoop(loop);
        return this;
    }

    /**
     * 设置数据项显示位置
     *
     * @param selection
     */
    public WheelViewDialog setSelection(int selection) {
        mWheelView.setSelection(selection);
        return this;
    }

    /**
     * 设置数据项
     *
     * @param arrays
     */
    public WheelViewDialog setItems(MapData[] arrays) {
        return setItems(Arrays.asList(arrays));
    }

    /**
     * 设置数据项
     *
     * @param list
     */
    public WheelViewDialog setItems(List<MapData> list) {
        mWheelView.setWheelData(list);
        return this;
    }

    /**
     * 显示
     */
    public WheelViewDialog show() {
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        return this;
    }

    /**
     * 隐藏
     */
    public WheelViewDialog dismiss() {
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
        return this;
    }

    @Override
    public void onClick(View v) {
        dismiss();
        if (null != mOnDialogItemClickListener) {
            mOnDialogItemClickListener.onItemClick(mSelectedPos, mSelectedText);
        }
    }

    public interface OnDialogItemClickListener<T> {
        void onItemClick(int position, T s);
    }

    public interface OnDialogBtnClickListener<T>{
        void onCancelBtnClick();
        void onComfirmBtnClick(T t);
    }

    public OnDialogBtnClickListener getmOnDialogBtnClickListener() {
        return mOnDialogBtnClickListener;
    }

    public void setmOnDialogBtnClickListener(OnDialogBtnClickListener mOnDialogBtnClickListener) {
        this.mOnDialogBtnClickListener = mOnDialogBtnClickListener;
    }
}
